package gui;

import sistema.Sistema;

import com.trolltech.qt.gui.QHBoxLayout;
import com.trolltech.qt.gui.QLabel;
import com.trolltech.qt.gui.QMessageBox;
import com.trolltech.qt.gui.QPixmap;
import com.trolltech.qt.gui.QPushButton;
import com.trolltech.qt.gui.QSpacerItem;
import com.trolltech.qt.gui.QVBoxLayout;
import com.trolltech.qt.gui.QWidget;
import com.trolltech.qt.gui.QSizePolicy.Policy;

import excecoes.ContaException;
import excecoes.SistemaException;

/**
 * Janela que mostra opcoes e dados de um amigo
 *
 */
public class FriendWindow extends QWidget {

	private final String IMG_PATH = "ImHereM03/img/";
	
	private Sistema sistema;
	private String userName;
	private String friendUserName;

	private QLabel friendName;
	private QLabel friendPhoto;
	private QLabel position;

	private QPushButton leaveButton;
	private QPushButton optionsButton;

	private QVBoxLayout mainLayout;
	private QHBoxLayout lay1;
	private QHBoxLayout lay2;

	/**
	 * Construtor
	 * 
	 * @param sistema
	 * @param userName
	 * @param friendUserName
	 */
	public FriendWindow(Sistema sistema, String userName, String friendUserName) {
		this.sistema = sistema;
		this.userName = userName;
		this.friendUserName = friendUserName;
		configureLayout();
	}

	/**
	 * Configura os layouts da janela
	 */
	private void configureLayout() {
		this.mainLayout = new QVBoxLayout();
		configureLayout1();
		configureLayout2();
		this.mainLayout.addLayout(lay1);
		this.setLayout(mainLayout);
	}

	/**
	 * Configura o primeiro layout
	 */
	private void configureLayout1() {
		this.optionsButton = new QPushButton("Opcoes");
		this.leaveButton = new QPushButton("Sair");
		QSpacerItem spacer = new QSpacerItem(0, 0, Policy.Expanding);
		lay2.addSpacerItem(spacer);
		lay2.addWidget(optionsButton);
		lay2.addWidget(leaveButton);
	}

	/**
	 * Configura o segundo layout
	 */
	private void configureLayout2() {
		try {
			friendName = new QLabel("Pagina de "
					+ sistema.getAtributoUsuario(friendUserName, "nome"));
			friendPhoto = new QLabel();
			QPixmap photo = new QPixmap(new QPixmap(sistema.getAtributoUsuario(
					friendUserName, "foto")));
			if (photo.isNull()) {
				photo = new QPixmap(IMG_PATH + "photo_default.png");
			}
			friendPhoto.setPixmap(photo);
			friendPhoto.setFixedSize(60, 80);
		} catch (ContaException e) {
			QMessageBox.critical(this, "Erro", e.getMessage());
			this.close();
		}
		this.lay2 = new QHBoxLayout();
		this.position = new QLabel("Localizacao bloqueada!");
		try {
			if (sistema.possoVerLocalizacao(userName, friendUserName)) {
				this.position.setText(sistema.getLocalizacao(friendUserName));
			}
		} catch (ContaException e) {
			QMessageBox.critical(this, "Erro", e.getMessage());
		} catch (SistemaException e) {
			QMessageBox.critical(this, "Erro", e.getMessage());
		}
		QSpacerItem spacer = new QSpacerItem(0, 0, Policy.Expanding, Policy.Expanding);
		QVBoxLayout aux = new QVBoxLayout();
		lay2.addWidget(friendPhoto);
		aux.addWidget(friendName);
		aux.addWidget(position);
		aux.addSpacerItem(spacer);
		aux.addLayout(aux);
		lay2.addSpacerItem(spacer);
	}

}
